<?php

/**
 * @version		$Id: hello.php 15 2009-11-02 18:37:15Z chdemko $
 * @package		Joomla16.Tutorials
 * @subpackage	Components
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @author		Christophe Demko
 * @link		http://joomlacode.org/gf/project/helloworld_1_6/
 * @license		License GNU General Public License version 2 or later
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');

/**
 * HelloWorld Model
 */
class bgModelbg extends JModel
{
    var $_total = null;
    var $_pagination = null;
    
    public function __construct(){
    	parent::__construct();
 
        $mainframe = JFactory::getApplication();
 
        // Get pagination request variables
        $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
 
        // In case limit has been changed, adjust it
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
 
        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
    }
    
	function getAll($list = null){

	   $qr = "SELECT * FROM mama";
       $where = array();
       // search
       if(isset($list['filter_search'])){
            $where[] = "MAMA_TITLE LIKE '%$list[filter_search]%'";
       }
       
       // filter
       if(isset($list['status']) && $list['status'] != 0){
            $where[] = "MAMA_FLAG = $list[status]";
       }

        if($where){
            $where = ' WHERE '.implode(' AND ', $where);
        }else{
            $where = null;
        }
       
       // get total number record
       $this->_total = $this->getTotal($where);
       
       $qr .= $where;
       
       // order by
       $qr .= " ORDER BY MAMA_CODE DESC"; 
       
       // pagination
       $qr .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');
        
       
       
        
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    
    function getTotal($where = null){
        // Load the content if it doesn't already exist
        if ($this->_total === null) {
            $query = "SELECT count(*) FROM mama $where";
            $this->_db->setQuery($query);
            $this->_total = $this->_db->loadResult();
        }
        
        return $this->_total;
    } 
    
    function getPagination(){
        // Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
    }
    
    
    function load($cid){
        $table = & JTable::getInstance('bg','bgTable');
        $table->load($cid);
        return $table;
    }
    
    public function savetemp($data, $addNew = false){
    	$table = & JTable::getInstance('bg','bgTable');
        $table->bind($data);
        
        if($table->store(false, $addNew)){
            return true;
        }
        
        return false;
    }
    
    public function remove($cid){
        $cid = implode(',', $cid);
    	$query = "DELETE FROM mama WHERE MAMA_CODE IN($cid)";
        $this->_db->setQuery($query);
        if($this->_db->query()){
            return true;
        }else{
            return false;
        }
    }
    
    public function updateFlag($flag, $id){
    	$qr = "UPDATE mama SET MAMA_FLAG = $flag WHERE MAMA_CODE = $id";
        $this->_db->setQuery($qr);
        if($this->_db->query()){
            return true;
        }else{
            return false;
        }
    }
    
    public function getMAGR_USERTYPE($id){
    	$query = "SELECT MAGR_USERTYPE FROM magr WHERE MAGR_CODE = $id";
        $this->_db->setQuery($query);
        return $this->_db->loadResult();
    }
    
    public function getUserSystem($idGroupMail){
    	$query =   "SELECT name, email FROM kc2vr_users
                        WHERE id IN 
                            (SELECT DISTINCT DEUM_USEM_COCH_CODE FROM deum
                                WHERE DEUM_MAGR_CODE = $idGroupMail)
                            AND sendEmail = 1";
        $this->_db->setQuery($query);
        
        return $this->_db->loadAssocList();
    }
    
    public function getUserCoacher($idGroupMail){
    	        $mail = "SELECT COCH_CODE name, COCH_MAIL email FROM coch
                        WHERE COCH_CODE IN
                            (SELECT DISTINCT DEUM_USEM_COCH_CODE FROM deum
                                WHERE DEUM_MAGR_CODE = $idGroupMail)
                            AND COCH_MAIF = 1";

        $mail_w = "SELECT COCH_CODE name, COCH_MAIW email FROM coch
                        WHERE COCH_CODE IN
                            (SELECT DISTINCT DEUM_USEM_COCH_CODE FROM deum
                                WHERE DEUM_MAGR_CODE = $idGroupMail)
                            AND COCH_MAFW = 1";

        $mail_k = "SELECT COCH_CODE name, COCH_MAIK email FROM coch
                        WHERE COCH_CODE IN
                            (SELECT DISTINCT DEUM_USEM_COCH_CODE FROM deum
                                WHERE DEUM_MAGR_CODE = $idGroupMail)
                            AND COCH_MAKF = 1";
        
        $query =   "$mail UNION $mail_w UNION $mail_k";

        $this->_db->setQuery($query);
        return $this->_db->loadAssocList();
    }
    
    public function getUserOut($idGroupMail){
    	$query =   "SELECT DUME_CODE name, DUME_MAIL email FROM dume
                        WHERE DUME_MAGR_CODE = $idGroupMail
                            AND DUME_FLAG = 1";
        $this->_db->setQuery($query);
        return $this->_db->loadAssocList();
    }
    
}
